package com.hdoit.ares.logical.acc.dao.impl;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;

import com.hdoit.ares.logical.acc.dao.IIncomeTaxDao;
import com.hdoit.ares.logical.acc.mapper.IncomeTaxMapper;
import com.hdoit.ares.logical.acc.model.AwardSetting;
import com.hdoit.ares.logical.acc.model.IncomeTax;
import com.hdoit.ares.logical.brokerage.model.CarBrokerageRecord;
import com.hdoit.ares.logical.brokerage.model.CarShareBrokerage;
import com.hdoit.ares.logical.cust.model.Customer;
import com.hdoit.ares.logical.viewmodel.acc.IncomeTaxSearch;
import com.tennetcn.common.dao.base.IFinderExpression;
import com.tennetcn.common.dao.base.impl.SuperDao;
import com.tennetcn.common.enums.OrderEnum;
import com.tennetcn.common.enums.pub.YesOrNo;
import com.tennetcn.common.message.PagerModel;
import com.tennetcn.common.utils.FinderExpressionFactory;
/**
 * @author      yujinhui
 * @email       
 * @createtime  2018年04月10日 16:35:50
 * @comment
 */
@Component
public class IncomeTaxDaoImpl extends SuperDao<IncomeTax> implements IIncomeTaxDao{
	
	private IncomeTaxMapper incomeTaxMapper;
	public IncomeTaxMapper getIncomeTaxMapper() {
		return incomeTaxMapper;
	}
	@Resource
	public void setIncomeTaxMapper(IncomeTaxMapper incomeTaxMapper) {
		this.incomeTaxMapper = incomeTaxMapper;
	}
	@Override
	public Double queryNetSalary(double money, String customerId) {
		
		return incomeTaxMapper.queryNetSalary(money, customerId);
	}
	
	
	@Override
	public List<IncomeTax> queryList(IncomeTaxSearch search,
			PagerModel pagerModel) {
		IFinderExpression finderExpression=FinderExpressionFactory.createExpression();
		finderExpression.select("incomeTax.*","customer.username as name","customer.nickname as nickName")
		                .from(IncomeTax.class,"incomeTax")
		                .leftJoin(Customer.class, "customer")
		                .on("incomeTax.customerId", "customer.id")
		                .addOrder("incomeTax.createTime", OrderEnum.desc);
		
		appendFinder(search,finderExpression);

        if(pagerModel == null){
            return queryList(finderExpression, search); 
          }
        return queryList(finderExpression, search, pagerModel);
		
	}
	private void appendFinder(IncomeTaxSearch search,
			IFinderExpression finderExpression) {
		
		if(search.getStartTime()!=null){
			finderExpression.andWhere("TIMESTAMPDIFF(SECOND,#{startTime},incomeTax.createTime)>=0")
				  			.setParam("startTime", search.getStartTime());
		}
		if(search.getEndTime()!=null){
			finderExpression.andWhere("TIMESTAMPDIFF(SECOND,#{endTime},incomeTax.createTime)<=0")
				  			.setParam("endTime", search.getEndTime());
		}
		if(!StringUtils.isEmpty(search.getName())){
			finderExpression.andWhere("customer.username like #{name}")
							.setParam("name", "%"+search.getName()+"%");
		}
		if(!StringUtils.isEmpty(search.getNickName())){
			finderExpression.andWhere("customer.nickname like #{nickName}")
							.setParam("nickName", "%"+search.getNickName()+"%");
		}
		
	}
	@Override
	public List<IncomeTax> queryList(IncomeTaxSearch search) {
		return queryList(search,null);
	}
	@Override
	public int queryCount(IncomeTaxSearch search) {
		IFinderExpression finderExpression=FinderExpressionFactory.createExpression();
		
		finderExpression.select("count(DISTINCT incomeTax.id) as count")
		                .from(IncomeTax.class,"incomeTax")
		                .leftJoin(Customer.class, "customer")
		                .on("incomeTax.customerId", "customer.id");
		
		appendFinder(search,finderExpression);
		
		return queryCount(finderExpression, search);

	}

}